package com.gsoe.judge.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClients;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ChatGmlUtil {


    static ClientV4 client=new ClientV4
            .Builder("01701903efc276d9ca56911b8d0df074.UNZW88RxyXOYTktT")
            .networkConfig(30,30,30,30, TimeUnit.SECONDS)
            .build();

    public static String testInvoke(List<String> messageStringList) {
        List<ChatMessage> messages = new ArrayList<>();
        for (String s : messageStringList) {
            ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), s);
            messages.add(chatMessage);
        }

        String requestId = String.format("GLM-4-0520", System.currentTimeMillis());

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(Boolean.FALSE)
                .invokeMethod(Constants.invokeMethod)
                .messages(messages)
                .requestId(requestId)
                .build();
        ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);
        return invokeModelApiResp.getData().getChoices().get(0).getMessage().getContent().toString();
    }
}
